<template>
    <el-dialog :title="!classData.classId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="classData" :rules="classRule" ref="classData" @keyup.enter.native="dataFormSubmit()"
            label-width="80px">
            <el-form-item label="班级" prop="classCode">
                <el-input v-model="classData.classCode" placeholder="班级"></el-input>
            </el-form-item>
            <!-- TODO 后期添加后台 获取角色为辅导员的列标 -->
            <el-form-item label="辅导员" prop="classLeader">
                <el-input v-model="classData.classLeader" placeholder="辅导员"></el-input>
            </el-form-item>
            <el-form-item   label="学制" prop="studentLength">
                <el-select class="select" v-model="classData.studentLength" placeholder="请选择">
                    <el-option key="2" label="2年" value="2">
                    </el-option>
                    <el-option key="3" label="3年" value="3">
                    </el-option>
                    <el-option key="4" label="4年" value="4">
                    </el-option>
                </el-select>
            </el-form-item>


            <el-form-item label="入学年份" prop="startYear">
                <el-date-picker class="select" value-format="yyyy" v-model="classData.startYear" type="year" placeholder="选择年">
                </el-date-picker>
            </el-form-item>


            <el-form-item label="所属专业" prop="deptId">
                <el-popover  ref="majorName" placement="bottom-start" trigger="click" >
                    <el-tree :data="majorList" :props="majorListTreeProps" node-key="majorId" ref="majorListTree"
                        @current-change="majortListTreeCurrentChangeHandle" :default-expand-all="true"
                        :highlight-current="true" :expand-on-click-node="false">
                    </el-tree>
                </el-popover>
                <el-input v-model="classData.major.majorName" v-popover:majorName :readonly="true" placeholder="点击选择学院"
                    class="menu-classData"></el-input>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { classApi, majorApi } from '@/api/baseinfo';
import { clone, treeDataToList } from '@/utils';

export default {
    components: {},
    props: {},
    data() {
        return {
            visible: false,
            classData: {
                classId: '',
                classCode: '',
                classLeader: '',
                majorId: '',
                startYear: '',
                studentLength: '',
                major: {
                    majorName: ''
                }
            },
            classRule: {
                parentName: { required: true, message: '所属学院不能为空', trigger: 'blur' },
                classCode: { required: true, message: '班级不能为空', trigger: 'blur' },
                // classLeader: { required: true, message: '辅导员不能为空', trigger: 'blur' },
                majorId: { required: true, message: '专业名称不能为空', trigger: 'blur' },
                startYear: { required: true, message: '入学年份不能为空', trigger: 'blur' },
                studentLength: { required: true, message: '学制不能为空', trigger: 'blur' },


            },
            majorList: [],
            majorListTreeProps: {
                label: 'majorName',
                children: 'list'
            }



        };
    },
    watch: {},
    computed: {},
    methods: {
        init(id) {
            majorApi.listMajorNoPage().then(({ data }) => {

                let cloneData = clone(data)
                this.majorList = cloneData
            }).then(() => {
                this.$nextTick(() => {
                    this.classData = this.$options.data().classData
                    this.$refs['classData'].clearValidate()
                })
                if (id) {
                    return Promise.resolve(classApi.getClassInfo(id))
                } else {
                    return Promise.resolve(false)
                }

            }).then((res) => {
                if (res) {
                    this.classData = res.class
                }
                this.$nextTick(() => {
                    this.visible = true
                })
                this.visible = true

            })






        },
        majortListTreeCurrentChangeHandle(data, node) {
            this.classData.majorId = data.majorId
            this.classData.major.majorName = data.majorName
        },

        dataFormSubmit() {
            this.$refs['classData'].validate((valid) => {

                if (valid) {
                    classApi.saveOrUpdate(this.classData, this.classData.classId ? 'update' : 'save').then(() => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                            }
                        })

                    })
                }
            })
        }
    },
    created() { },
    mounted() { }
};
</script>

<style lang="scss">
.select{
    width: 100% !important;
}

</style>

<style lang="scss" scoped>

</style>